package com.ruix.app.mapper;

import com.hyphenate.chat.EMImageMessageBody;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.chat.EMVoiceMessageBody;
import com.hyphenate.chat.adapter.message.EMAImageMessageBody;
import com.hyphenate.chat.adapter.message.EMAMessage;
import com.hyphenate.chat.adapter.message.EMAMessageBody;
import com.hyphenate.chat.adapter.message.EMATextMessageBody;
import com.hyphenate.chat.adapter.message.EMAVoiceMessageBody;
import com.ruix.app.manager.UserManager;
import com.ruix.app.util.LogUitl;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by hadexs on 2017/5/28.
 */

public class MessageMapper implements ApiMapper<ArrayList<EMMessage>>{


    @Override
    public ArrayList<EMMessage> transform(JSONObject response) {
        ArrayList<EMMessage> messagelist= new ArrayList();;
        String uid = UserManager.getCurrentUser().getID();
        if (response != null){
           JSONObject data =  response.optJSONObject("data");
            if (data != null){
              JSONArray list = data.optJSONArray("list");
                if (list != null && list.length() > 0){
                    for (int i = 0; i < list.length(); i++) {
                        JSONObject msgobj =  list.optJSONObject(i);
                        String msgid = msgobj.optString("id", "");
                        String from = msgobj.optString("from", "");
                        String type = msgobj.optString("type", "");
                        String msg = msgobj.optString("msg", "");
                        String url = msgobj.optString("url", "");
                        int length = msgobj.optInt("length", 1);
                        int width = msgobj.optInt("width", 0);
                        int height = msgobj.optInt("height", 0);
                        int hasLike = msgobj.optInt("has_like", 0);
                        int likeCnt = msgobj.optInt("like_cnt", 0);
                        boolean isRead = msgobj.optInt("is_read",1) ==1;

                        EMMessage emMessage = null;
                        if (from.equals(uid)){
                            emMessage = EMMessage.createSendMessage(EMMessage.Type.TXT);
                            emMessage.setDirection(EMMessage.Direct.SEND);
                        } else {
                            emMessage = EMMessage.createReceiveMessage(EMMessage.Type.TXT);
                            emMessage.setDirection(EMMessage.Direct.RECEIVE);
                        }


                        if ("txt".equals(type)){
                            EMTextMessageBody body = new EMTextMessageBody(msg);
                            emMessage.addBody(body);
                        } else if ("img".equals(type)){
//                            emMessage = EMMessage.createReceiveMessage(EMMessage.Type.IMAGE);
                            String thumbSmall = url + "?x-oss-process=style/thumb_small";
                            EMAImageMessageBody emaImageMessageBody = new EMAImageMessageBody(url,thumbSmall);
                            emaImageMessageBody.setThumbnailRemotePath(thumbSmall);
//                            emaImageMessageBody.setThumbnailSize(width,height);
                            EMImageMessageBody body = new EMImageMessageBody(emaImageMessageBody);
                            body.setLocalUrl(url);
                            body.setRemoteUrl(url);
//                            body.setThumbnailUrl();
                            emMessage.addBody(body);
                        } else if ("audio".equals(type)){
//                            emMessage = EMMessage.createSendMessage(EMMessage.Type.VOICE);
                            EMAVoiceMessageBody body = new EMAVoiceMessageBody(url,length);
                            body.setDuration(length);
                            body.setRemotePath(url);
                            body.setLocalPath(url);
                            EMVoiceMessageBody voiceMessageBody = new EMVoiceMessageBody(body);
                            voiceMessageBody.setRemoteUrl(url);
                            voiceMessageBody.setLocalUrl(url);
                            emMessage.addBody(voiceMessageBody);
                        }

                        if (emMessage == null){
                            break;
                        }
                        emMessage.setListened(isRead);
                        emMessage.setMsgId(msgid);
                        emMessage.setStatus(EMMessage.Status.SUCCESS);
//                        if ("text".equals(type)){
//                            EMTextMessageBody body = new EMTextMessageBody(msg);
////                            emMessage = EMMessage.createReceiveMessage(EMMessage.Type.TXT);
//                            emMessage = EMMessage.createSendMessage(EMMessage.Type.TXT);
//                            emMessage.addBody(body);
//                        } else if ("img".equals(type)){
//                            emMessage = EMMessage.createSendMessage(EMMessage.Type.IMAGE);
//                            String thumbSmall = url + "?x-oss-process=style/thumb_small";
//                            EMImageMessageBody body = new EMImageMessageBody(url,thumbSmall);
//                            body.setThumbnailRemotePath(url);
//                            body.setThumbnailSize(width,height);
//                            emamessage.addBody(body);
//                        } else if ("audio".equals(type)){
//                            emMessage = EMMessage.createSendMessage(EMMessage.Type.VOICE);
//                            EMAVoiceMessageBody body = new EMAVoiceMessageBody(url,length);
//                            emamessage.addBody(body);
//                        }
                        emMessage.setChatType(EMMessage.ChatType.GroupChat);
                        emMessage.setFrom(from);
                        emMessage.setTo("");
                        emMessage.setMsgTime(msgobj.optInt("created_at"));
                        emMessage.setStatus(EMMessage.Status.SUCCESS);
                        JSONObject ext =   msgobj.optJSONObject("ext");
                        if (ext != null){
                            String identif = ext.optString("identifier","");
                            if (identif != null) {

                                emMessage.setAttribute("identifier", identif);
                            }
                            String exttype = ext.optString("type");
                            if (emMessage != null){
                                emMessage.setAttribute("type",exttype);
                            }
                            JSONObject author  = ext.optJSONObject("author");
                            if (author != null) {
                                emMessage.setAttribute("author", author);
                            }
                            boolean ignore_notification = ext.optBoolean("em_ignore_notification",true);
                            emMessage.setAttribute("em_ignore_notification",ignore_notification);
                            emMessage.setAttribute("has_like",hasLike);
                            emMessage.setAttribute("like_cnt",likeCnt);
                            emMessage.setAttribute("message_from","history");
                        }
                        if (emMessage != null){
                            messagelist.add(emMessage);
                        }
//                        EMMessage emmessage =  EMMessage.createTxtSendMessage(); new EMMessage(emamessage);
//                        emmessage.getType()
//                        messagelist.add(emmessage);
                    }

                }

            }

        }


        return messagelist;
    }
}
